package com.tuoren.forward.controller;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tuoren.forward.annotation.HandleLog;
import com.tuoren.forward.constant.CommonConstant;
import com.tuoren.forward.entity.Hospital;
import com.tuoren.forward.entity.User;
import com.tuoren.forward.entity.req.HospitalAddOpenReq;
import com.tuoren.forward.entity.req.UserAddOpenReq;
import com.tuoren.forward.entity.resp.CaptchaResp;
import com.tuoren.forward.service.HospitalService;
import com.tuoren.forward.service.UserService;
import com.tuoren.forward.util.ResultData;
import com.tuoren.forward.util.UUIDUtil;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;
import cn.hutool.captcha.generator.MathGenerator;
import cn.hutool.crypto.digest.DigestUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping("open")
@Tag(name="开放接口")
public class OpenController {
	
	@Autowired
	RedisTemplate<String, Object> redisTemplate;
	
	@Autowired
	HospitalService hospitalService;
	
	@Autowired
	UserService userService;
	
	private final String SECRET = "yunzhineng";
	
//	@PostMapping("addHospital")
//    @ResponseBody
//    @Operation(summary = "添加医院")
	public ResultData<Hospital> addHospital(@RequestBody HospitalAddOpenReq req){
		log.info("addHospital>>:{}",req);
		String content= new StringBuffer().append(req.getId()).append("&").append(req.getName())
				.append("&").append(SECRET).toString();
		String md5Hex1 = DigestUtil.md5Hex(content);
		Hospital record = new Hospital();
		if(!req.getMd5().equals(md5Hex1)) {
			return ResultData.failNull();
		}
		
		BeanUtils.copyProperties(req, record);
		return hospitalService.addBack(record);
	}
	
	@HandleLog
	@PostMapping("addUser")
    @ResponseBody
    @Operation(summary = "添加用户")
	public ResultData<User> addUser(@RequestBody UserAddOpenReq req){
		log.info("addUser>>:{}",req);
		String content= new StringBuffer(req.getUsername()).append("&").append(SECRET).toString();
		String md5Hex1 = DigestUtil.md5Hex(content);
		if(!req.getMd5().equals(md5Hex1)) {
			return ResultData.failNull();
		}
		return userService.addBack(req);
	}
	
	@PostMapping("getCaptcha")
    @ResponseBody
    @Operation(summary = "获取验证码")
	public ResultData<CaptchaResp> getCaptcha(){
		log.info("getCaptcha>>");
		String captchaKey = UUIDUtil.get32UUID();
		ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(150, 40, 4, 0);
		captcha.setGenerator(new MathGenerator(1));
		captcha.createCode();
		redisTemplate.opsForHash().put("captcha", captchaKey, captcha.getCode());
		redisTemplate.expire(CommonConstant.CAPTCHA_REDIS_HASH, 2,TimeUnit.MINUTES); //2分钟过期
		String base64String = "data:image/png;base64,"+captcha.getImageBase64();
		CaptchaResp resp = new CaptchaResp();
		resp.setCaptchaKey(captchaKey);
		resp.setCaptchaImg(base64String);
		return ResultData.success(resp);
	}
	
	public static void main(String[] args) {
		String content="test3&yunzhineng";
		String md5Hex1 = DigestUtil.md5Hex(content);
		MathGenerator ge = new MathGenerator();
		boolean f = ge.verify("9+9","18");
		System.out.println(md5Hex1+f);
	}

}
